package com.gws.ievent.db.service;

import com.gws.ievent.db.dao.EventOnlyDao;
import com.gws.ievent.db.model.EventOnlyModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.Date;

/**
 * @author song
 * @date 2022/10/23 16:17
 */
@Service
public class EventOnlyService {

    @Autowired
    private EventOnlyDao eventOnlyDao;

    @Value(value = "${server.port}")
    private int port;


    /**
     * 添加事件唯一值
     *
     * @param eventId
     * @param onlyId
     * @return
     */
    public boolean create(long eventId, String onlyId) {
        EventOnlyModel model = new EventOnlyModel();
        model.setEventId(eventId);
        model.setOnlyId(onlyId);
        model.setCreatedAt(new Date());

        try {
            model.setServerName(InetAddress.getLocalHost() + ":" + this.port);
            return eventOnlyDao.save(model);
        } catch (Exception e) {
            return false;
        }
    }

}
